/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlysieuthi;

import DTO.CUser_group;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import DAO.CUserGroupDAO;
import DAO.SubConfig;

public class FrmUserGroup extends javax.swing.JFrame {

    /**
     * Creates new form FrmUserGroup
     */
    public FrmUserGroup() {
        initComponents();
        loadDataGirdView();
    }
    int currentIndex = 0;
    int numberRow = 0;
    Boolean add = false;
    Boolean edit = false;
    List<CUser_group> luserGroup = new ArrayList<CUser_group>();
    CUser_group current = null;
    SubConfig config = null;
    private void view(int index){
        setEdittable(false);
        current = null;
        if(index >= 0 && index < numberRow){
            CUser_group item = this.luserGroup.get(index);
            current = item;
            this.txtTengroup.setText(item.getTenGroup());
            this.txtRows.setText(String.format("%s/%s", index + 1,numberRow));
        }
        else{
            this.txtRows.setText(String.format("0/0"));
        }
    }
    
    private void clearText(){
        this.txtTengroup.setText("");
    }
    
    private void setEdittable(Boolean b){
        this.txtTengroup.setEditable(b);
        this.txtRows.setEditable(false);
    }
    
    private void isSetEnabled(Boolean b){
        this.btnadd.setEnabled(b);
        this.btnCanCel.setEnabled(!b);
        this.btndelete.setEnabled(b);
        this.btnedit.setEnabled(b);
        this.btnsave.setEnabled(!b);
    }
    
    private Boolean addNew(){
        if(this.txtTengroup.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Không được bỏ trống.");
            return false;
        }
        CUserGroupDAO ugDAO = new CUserGroupDAO();
        config = SubConfig.getInstance();
        config.init();
        CUser_group us = new CUser_group();
        us.setTenGroup(this.txtTengroup.getText());
        int id =(int)ugDAO.insertOnSubmit(us, config);        
        if(id >=1 ){
            JOptionPane.showMessageDialog(this, "Them moi thanh cong.");
        }
        config.close();
        this.currentIndex = 0;
        loadDataGirdView();
        return true;
    }
    
    private Boolean editRow(){
        if(this.txtTengroup.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Không được bỏ trống.");
            return false;
        }
        CUserGroupDAO ugDAO = new CUserGroupDAO();
        config = SubConfig.getInstance();
        config.init();
        this.current.setTenGroup(this.txtTengroup.getText());
        int t =(int)ugDAO.updateOnSubmint(current, config);
        config.close();
        if(t>=1){
            JOptionPane.showMessageDialog(this, "Cap nhat thanh cong");
        }
        return true;
    }
    
    private void loadDataGirdView(){
        CUserGroupDAO ugDAO = new CUserGroupDAO();
        config = SubConfig.getInstance();
        config.init();
        this.luserGroup = ugDAO.getAll(config);
        numberRow = this.luserGroup.size();        
        config.close();
        this.view(this.currentIndex);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtTengroup = new javax.swing.JTextField();
        btnPrevAll = new javax.swing.JButton();
        btnPrev = new javax.swing.JButton();
        txtRows = new javax.swing.JTextField();
        btnNext = new javax.swing.JButton();
        btnNextAll = new javax.swing.JButton();
        btnadd = new javax.swing.JButton();
        btnedit = new javax.swing.JButton();
        btnsave = new javax.swing.JButton();
        btnCanCel = new javax.swing.JButton();
        btndelete = new javax.swing.JButton();
        btnclose = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setText("Tên nhóm người dùng");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(jLabel1)
                .addGap(48, 48, 48)
                .addComponent(txtTengroup, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtTengroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        btnPrevAll.setText("<<");
        btnPrevAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevAllActionPerformed(evt);
            }
        });

        btnPrev.setText("<");
        btnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevActionPerformed(evt);
            }
        });

        btnNext.setText(">");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnNextAll.setText(">>");
        btnNextAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextAllActionPerformed(evt);
            }
        });

        btnadd.setText("Thêm mới");
        btnadd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaddActionPerformed(evt);
            }
        });

        btnedit.setText("Sửa");
        btnedit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditActionPerformed(evt);
            }
        });

        btnsave.setText("Lưu");
        btnsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });

        btnCanCel.setText("Huỷ");
        btnCanCel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCanCelActionPerformed(evt);
            }
        });

        btndelete.setText("Xóa");
        btndelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndeleteActionPerformed(evt);
            }
        });

        btnclose.setText("Thoát");
        btnclose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncloseActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(102, 102, 255));
        jLabel7.setText("QUẢN LÝ NHÓM NGƯỜI DÙNG");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel7)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnadd)
                        .addGap(7, 7, 7)
                        .addComponent(btnedit)
                        .addGap(7, 7, 7)
                        .addComponent(btnsave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCanCel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btndelete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnclose)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(184, 184, 184)
                .addComponent(btnPrevAll)
                .addGap(18, 18, 18)
                .addComponent(btnPrev)
                .addGap(28, 28, 28)
                .addComponent(txtRows, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(btnNext)
                .addGap(18, 18, 18)
                .addComponent(btnNextAll)
                .addGap(66, 66, 66))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(jLabel7)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPrevAll)
                    .addComponent(txtRows, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnNext)
                    .addComponent(btnPrev)
                    .addComponent(btnNextAll))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnedit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnsave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnclose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnadd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btndelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCanCel))
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnPrevAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevAllActionPerformed
        // TODO add your handling code here:
        this.currentIndex = 0;
        this.view(this.currentIndex);
    }//GEN-LAST:event_btnPrevAllActionPerformed

    private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevActionPerformed
        // TODO add your handling code here:
        if(this.currentIndex > 0){
            this.currentIndex -= 1;
            this.view(this.currentIndex);
        }
    }//GEN-LAST:event_btnPrevActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // TODO add your handling code here:
        if(this.currentIndex < this.numberRow){
            this.currentIndex += 1;
            this.view(this.currentIndex);
        }
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnNextAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextAllActionPerformed
        // TODO add your handling code here:
        this.currentIndex = this.numberRow - 1;
        this.view(this.currentIndex);
    }//GEN-LAST:event_btnNextAllActionPerformed

    private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnaddActionPerformed
        // TODO add your handling code here:
        this.add = true;
        this.clearText();
        this.edit = false;        
        this.setEdittable(true);
        this.isSetEnabled(false);
    }//GEN-LAST:event_btnaddActionPerformed

    private void btneditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneditActionPerformed
        // TODO add your handling code here:
        this.add = false;
        this.edit = true;        
        this.btnadd.setEnabled(false);
        this.btnsave.setEnabled(true);
        this.btndelete.setEnabled(false);
        this.btndelete.setEnabled(false);
    }//GEN-LAST:event_btneditActionPerformed

    private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsaveActionPerformed
        // TODO add your handling code here:

        Boolean flag = false;
        if(this.add){
            flag = this.addNew();
        }
        else if(this.edit){
           flag = editRow();
        }
        if(flag){
            this.btnedit.setEnabled(true);
            this.btnadd.setEnabled(true);
            this.btnsave.setEnabled(false);
            this.btndelete.setEnabled(true);            
        }
    }//GEN-LAST:event_btnsaveActionPerformed

    private void btnCanCelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCanCelActionPerformed
        // TODO add your handling code here:        
        this.view(this.currentIndex);
        this.add = false;
        this.edit = false;
    }//GEN-LAST:event_btnCanCelActionPerformed

    private void btndeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btndeleteActionPerformed
        // TODO add your handling code here:
        /*
        int reply = JOptionPane.showConfirmDialog(this, "Bạn có muốn xóa user này không ?");
        if(reply == JOptionPane.YES_OPTION){
           
        }*/
    }//GEN-LAST:event_btndeleteActionPerformed

    private void btncloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncloseActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btncloseActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmUserGroup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmUserGroup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmUserGroup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmUserGroup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmUserGroup().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCanCel;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnNextAll;
    private javax.swing.JButton btnPrev;
    private javax.swing.JButton btnPrevAll;
    private javax.swing.JButton btnadd;
    private javax.swing.JButton btnclose;
    private javax.swing.JButton btndelete;
    private javax.swing.JButton btnedit;
    private javax.swing.JButton btnsave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtRows;
    private javax.swing.JTextField txtTengroup;
    // End of variables declaration//GEN-END:variables
}
